package com.bicon.botu.mall.component.controller;

import com.bicon.botu.mall.component.service.IAppVersionService;
import com.bicon.botu.core.base.common.controller.BaseController;
import com.bicon.botu.core.base.common.entity.BaseResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: 吕祥
 * @Date: Created in 16:26 2017/10/17/017
 */
@RestController
@RequestMapping("/V1.0.0/base/appVersion")
public class AppVersionController extends BaseController {

    @Autowired
    IAppVersionService iAppVersionService;

    /**
     * 新增版本
     *
     * @param forcedUpdatingState 强制更新状态(0:非强制，1:强制)
     * @param type                类型：iOS,Android
     * @param versionName         版本名称
     * @param versionCode         版本号
     * @param url                 下载地址
     * @param info                版本说明
     * @param size                文件大小
     * @param times               次数
     * @param state               状态:0未发布，1已发布
     * @return
     */
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public BaseResult saveAppVersion(@RequestParam String type, @RequestParam String versionName, @RequestParam Integer versionCode,
                                     @RequestParam String url, @RequestParam String info, @RequestParam Integer size,
                                     @RequestParam(defaultValue = "0") Integer times, @RequestParam(defaultValue = "0") String state,
                                     @RequestParam(defaultValue = "0") String forcedUpdatingState) {
        if (StringUtils.isAnyBlank(type, versionName, String.valueOf(versionCode), url, info,
                String.valueOf(size), String.valueOf(times), state, forcedUpdatingState)) {
            return sendResult400();
        }
        return sendResult200(iAppVersionService.saveAppVersion(type, versionName, versionCode, url, info, size, times, state, forcedUpdatingState));
    }

    /**
     * 修改版本
     *
     * @param forcedUpdatingState 强制更新状态(0:非强制，1:强制)
     * @param id                  主键id
     * @param type                类型：iOS,Android
     * @param versionName         版本名称
     * @param versionCode         版本号
     * @param url                 下载地址
     * @param info                版本说明
     * @param size                文件大小
     * @param times               次数
     * @param state               状态:0未发布，1已发布
     * @return
     */
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public BaseResult updateAppVersion(@RequestParam String id, @RequestParam(required = false) String type, @RequestParam(required = false) String versionName,
                                       @RequestParam(required = false) Integer versionCode, @RequestParam(required = false) String url,
                                       @RequestParam(required = false) String info, @RequestParam(required = false) Integer size,
                                       @RequestParam(required = false) Integer times, @RequestParam(required = false) String state,
                                       @RequestParam(required = false) String forcedUpdatingState) {
        if (StringUtils.isAnyBlank(id)) {
            return sendResult400();
        }
        return sendResult200(iAppVersionService.updateAppVersion(id, type, versionName, versionCode, url, info, size, times, state, forcedUpdatingState));
    }

    /**
     * 删除app版本
     *
     * @param id 主键id
     * @return
     */
    @RequestMapping(value = "delete", method = RequestMethod.POST)
    public BaseResult deleteAppVersion(@RequestParam String id) {
        if (StringUtils.isAnyBlank(id)) {
            return sendResult400();
        }
        return sendResult200(iAppVersionService.deleteAppVersion(id));
    }

    /**
     * 根据id查询版本详情
     *
     * @param id 主键id
     * @return
     */
    @RequestMapping(value = "findOne", method = RequestMethod.POST)
    public BaseResult findOneAppVersion(@RequestParam String id) {
        if (StringUtils.isAnyBlank(id)) {
            return sendResult400();
        }
        return sendResult200(iAppVersionService.findOneAppVersion(id));
    }

    /**
     * 分页查询app版本
     *
     * @param pageNo   页数
     * @param pageSize 每页记录数
     * @return
     */
    @RequestMapping(value = "page", method = RequestMethod.POST)
    public BaseResult appVersionPage(@RequestParam(defaultValue = "1") Integer pageNo, @RequestParam(defaultValue = "5") Integer pageSize) {
        if (0 >= pageNo || 0 >= pageSize) {
            return sendResult400();
        }
        return sendResult200(iAppVersionService.appVersionPage(pageNo, pageSize));
    }
}
